const { Service } = require('egg')

class IndexService extends Service {
  async find(name) {
    return await this.ctx.model.Default.find({ name })
  }

  async status(user) {
    const model = this.ctx.model
    if (user) {
      return {
        serverCount: await model.Server.count(user),
        streamCount: await model.Stream.count(user),
        errorCount: await model.Error.count(user),
        appCount: await model.Application.count(user),
      }
    }
    return {
      serverCount: await model.Server.count(),
      streamCount: await model.Stream.count(),
      errorCount: await model.Error.count(),
      appCount: await model.Application.count(),
    }
  }
}

module.exports = IndexService